package com.woniuxy.woniubookmanager.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.woniubookmanager.entity.Book;
import com.woniuxy.woniubookmanager.result.ResponseResult;
import com.woniuxy.woniubookmanager.service.BookService;
import com.woniuxy.woniubookmanager.vo.BookVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wugd
 * @since 2022-11-29 04:03:13
 */
@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    BookService bookService ;

    @Value("${fileUploadPath}")
    private String fileUploadPath ;

    @GetMapping("/blist")
    public ResponseResult<Page<BookVo>> list(@RequestParam(value = "now",defaultValue = "1")Integer now,
                                             @RequestParam(value = "size",defaultValue = "3")Integer size,
                                             String bname){
        ResponseResult<Page<BookVo>> result = null;
        try {
            Page<BookVo> page = bookService.getBookByPage(now,size,bname);
            result = new ResponseResult<>(200,"ok",page);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResponseResult<>(201,"查询失败!");
        }

        return result ;
    }

    @RequestMapping("/uploadImg")
    public ResponseResult<String> uploadImg(@RequestParam("uploadImg") MultipartFile uploadImg){
        ResponseResult<String> result = null;
        try {
            //获取上传文件的名字
            String filename = uploadImg.getOriginalFilename();
            //修改文件名字
            String s = UUID.randomUUID().toString().replaceAll("-", "");
            filename = "WONIU"+ s + filename.substring(filename.lastIndexOf("."));
            System.out.println("新的图片名称:"+filename);

            //获取上传的路径
            File f = new File(this.fileUploadPath+filename) ;
            uploadImg.transferTo(f); //上传
            String fileUrl = "http://localhost:10001/imgs/"+filename ;
            result = new ResponseResult<>(200,"ok",fileUrl);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResponseResult<>(201,"上传失败!");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result ;
    }

    @PostMapping("/addBook")
    public ResponseResult<Void> addBook(@RequestBody Book book){
        ResponseResult<Void> result = null;
        try {
            book.setCreatetime(new Date());
            bookService.save(book);
            result = new ResponseResult<>(200,"ok");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResponseResult<>(201,"保存失败！");
        }
        return result ;
    }

}

